﻿package com.tudou.home.controller
{
    import com.tudou.ad.consts.*;
    import com.tudou.consts.*;
    import com.tudou.cookie.*;
    import com.tudou.home.commands.*;
    import com.tudou.home.commands.ad.*;
    import com.tudou.home.consts.*;
    import com.tudou.home.model.*;
    import com.tudou.home.newskin.interfaces.*;
    import com.tudou.home.track.*;
    import com.tudou.home.utils.*;
    import com.tudou.home.view.*;
    import com.tudou.newPlayer.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.command.*;

    public class PlayerCommand extends SimpleCommand implements ICommand
    {

        public function PlayerCommand()
        {
            return;
        }// end function

        private function processSizeRate(param1:int) : void
        {
            if (StatusInfo.getInst().hasSelectRate)
            {
                return;
            }
            var _loc_2:* = PlayerSO.getInst();
            var _loc_3:* = PageInfo.getInstance();
            var _loc_4:* = _loc_3.maxRateType;
            var _loc_5:* = _loc_3.original;
            var _loc_6:* = _loc_2.rateSetting;
            var _loc_7:* = _loc_3.currRateType;
            if (_loc_6.type == RateSettingData.SIZE)
            {
                if (param1 == ViewState.NORMAL)
                {
                    _loc_7 = RateType.P360;
                }
                else if (param1 == ViewState.EXPAND)
                {
                    _loc_7 = RateType.P480;
                }
                else if (param1 == ViewState.FULLSCREEN)
                {
                    if (PlayerConsts.FULLSCREEN_RECT.height > 480 && _loc_3.original)
                    {
                        _loc_7 = RateType.ORIGINAL;
                    }
                    else
                    {
                        _loc_7 = RateType.P480;
                    }
                }
                else
                {
                    return;
                }
                _loc_7 = RateUtil.correct(_loc_7, _loc_4, _loc_5);
                if (_loc_7 > _loc_3.currRateType)
                {
                    sendNotification(PlayerCommandSet.CHANGE_RATE_TYPE, _loc_7);
                }
            }
            return;
        }// end function

        private function get skipHeadValue() : Number
        {
            var _loc_1:* = PageInfo.getInstance();
            var _loc_2:* = PlayerSO.getInst();
            var _loc_3:* = _loc_2.skipSetting;
            var _loc_4:* = _loc_3 && _loc_3.skipIntro ? (_loc_1.skipHeadValue) : (0);
            return _loc_3 && _loc_3.skipIntro ? (_loc_1.skipHeadValue) : (0);
        }// end function

        private function get skipTailValue() : Number
        {
            var _loc_1:* = PageInfo.getInstance();
            var _loc_2:* = PlayerSO.getInst();
            var _loc_3:* = _loc_2.skipSetting;
            var _loc_4:* = _loc_3 && _loc_3.skipOutro ? (_loc_1.skipTailValue) : (0);
            return _loc_3 && _loc_3.skipOutro ? (_loc_1.skipTailValue) : (0);
        }// end function

        private function preStartPlay() : void
        {
            var _loc_1:* = StatusInfo.getInst();
            _loc_1.ad = StatusInfo.AD_FRONT_SHOWED;
            if (_loc_1.v2 == StatusInfo.V2_UNPASSED)
            {
                sendNotification(PrivacyCommandSet.WRONG_PASSWORD);
            }
            return;
        }// end function

        private function processRateSetting() : void
        {
            var _loc_1:* = facade.retrieveMediator(SkinMediator.NAME) as SkinMediator;
            var _loc_2:* = PlayerSO.getInst();
            var _loc_3:* = PageInfo.getInstance();
            var _loc_4:* = _loc_3.maxRateType;
            var _loc_5:* = _loc_3.original;
            var _loc_6:* = _loc_2.rateSetting;
            var _loc_7:int = 0;
            if (_loc_6.type == RateSettingData.SIZE)
            {
                if (_loc_3.viewState == ViewState.NORMAL)
                {
                    _loc_7 = RateType.P360;
                }
                else if (_loc_3.viewState == ViewState.EXPAND)
                {
                    _loc_7 = RateType.P480;
                }
                else if (_loc_3.viewState == ViewState.FULLSCREEN)
                {
                    if (PlayerConsts.FULLSCREEN_RECT.height > 480 && _loc_3.original)
                    {
                        _loc_7 = RateType.ORIGINAL;
                    }
                    else
                    {
                        _loc_7 = RateType.P480;
                    }
                }
            }
            else if (_loc_6.type == RateSettingData.ALWAYS_RATE)
            {
                _loc_7 = _loc_6.rateType;
            }
            else
            {
                _loc_7 = _loc_4;
                if (_loc_7 > RateType.P256)
                {
                    _loc_7 = RateType.P360;
                }
            }
            _loc_7 = RateUtil.correct(_loc_7, _loc_4, _loc_5);
            _loc_1.setRateSetting(_loc_6);
            if (_loc_7 != _loc_3.currRateType)
            {
                sendNotification(PlayerCommandSet.CHANGE_RATE_TYPE, _loc_7);
            }
            return;
        }// end function

        private function get lastCurPlayTimePos() : Number
        {
            var _loc_1:* = PageInfo.getInstance();
            var _loc_2:* = _loc_1.lastCurPlayTimePos;
            return _loc_2;
        }// end function

        override public function execute(param1:INotification) : void
        {
            var _loc_2:PlayerProxy = null;
            var _loc_10:Object = null;
            var _loc_11:Object = null;
            var _loc_12:Boolean = false;
            var _loc_13:Number = NaN;
            var _loc_14:Number = NaN;
            var _loc_15:int = 0;
            var _loc_16:int = 0;
            var _loc_17:Boolean = false;
            var _loc_18:int = 0;
            var _loc_19:PageProxy = null;
            var _loc_20:RateSettingData = null;
            var _loc_21:SkipSettingData = null;
            var _loc_22:Array = null;
            var _loc_23:Boolean = false;
            _loc_2 = facade.retrieveProxy(PlayerProxy.NAME) as PlayerProxy;
            var _loc_3:* = facade.retrieveMediator(SkinMediator.NAME) as SkinMediator;
            var _loc_4:* = AOPTracker.getInst();
            var _loc_5:* = PlayerSO.getInst();
            var _loc_6:* = PageInfo.getInstance();
            var _loc_7:* = _loc_2.playerStatus;
            var _loc_8:Number = 0;
            var _loc_9:* = param1.getType();
            _loc_10 = param1.getBody();
            switch(param1.getName())
            {
                case PlayerCommandSet.PLAYER_INITED:
                {
                    initPlayer();
                    break;
                }
                case PlayerCommandSet.PLAYER_CHANGE:
                {
                    _loc_3.changeVideo(_loc_10 as DisplayObject);
                    _loc_3.setPreviewer(_loc_2.previewPlayer);
                    break;
                }
                case PlayerCommandSet.UPDATE:
                {
                    _loc_3.setUpdate(_loc_10 as Object);
                    break;
                }
                case PlayerCommandSet.NOTIFY_SKIN_STATE:
                {
                    _loc_3.setPlayState(int(_loc_10));
                    break;
                }
                case PlayerCommandSet.PLAYER_EVENT:
                {
                    _loc_3.setPlayerEvent(String(_loc_10));
                    break;
                }
                case PlayerCommandSet.FIRST_BUFFER_FULL:
                {
                    sendNotification(AdLoadingCommandSet.CLEAR);
                    sendNotification(AdBackgroundCommandSet.START);
                    sendNotification(AdOverlayCommandSet.START);
                    sendNotification(AdCornerCommandSet.START);
                    UserLog.getInst().addNode(NodeLog.VIDEO_FIRST_BUFFER_FULL);
                    break;
                }
                case PlayerCommandSet.START_LOAD:
                {
                    _loc_2.load();
                    break;
                }
                case PlayerCommandSet.START_PLAY:
                {
                    sendNotification(AdPlacardCommandSet.CLEAR);
                    sendNotification(AdFrontCommandSet.CLEAR);
                    if (!_loc_7.hasReady)
                    {
                        sendNotification(AdLoadingCommandSet.START);
                    }
                    _loc_2.play();
                    sendNotification(AdCommandSet.AD_CPT_FLOAT_SHOW);
                    preStartPlay();
                    break;
                }
                case PlayerCommandSet.TIMEOUT_START_PLAY:
                {
                    sendNotification(AdFrontMultiCommandSet.TIMEOUT_START_PLAY);
                    break;
                }
                case PlayerCommandSet.PLAY:
                {
                    sendNotification(AdPauseCommandSet.CLEAR);
                    _loc_2.resume();
                    break;
                }
                case PlayerCommandSet.PAUSE:
                {
                    if (_loc_7.isPlaying || _loc_7.isBuffering)
                    {
                        _loc_2.pause();
                        ADTrackSender.getInstance().adExpect(AdType.PAUSE);
                        sendNotification(AdPauseCommandSet.START);
                    }
                    break;
                }
                case PlayerCommandSet.TOGGLE_PAUSE:
                {
                    if (_loc_7.isPlaying || _loc_7.isBuffering)
                    {
                        _loc_23 = false;
                        if (_loc_10 != null)
                        {
                            _loc_23 = _loc_10 as Boolean;
                        }
                        _loc_6.isForceBand = _loc_23;
                        sendNotification(PlayerCommandSet.PAUSE);
                    }
                    else
                    {
                        sendNotification(PlayerCommandSet.PLAY);
                    }
                    break;
                }
                case PlayerCommandSet.SEEK:
                {
                    _loc_11 = _loc_10 as Object;
                    if (_loc_7.isCompleted)
                    {
                        sendNotification(PlayerCommandSet.HIDE_PLAY_COMPLETE);
                    }
                    _loc_2.seek(_loc_11.seekPoint, _loc_11.isDraging);
                    break;
                }
                case PlayerCommandSet.MUTE:
                {
                    if (_loc_10 != null)
                    {
                        _loc_12 = _loc_10 as Boolean;
                    }
                    else
                    {
                        _loc_12 = !_loc_2.mute;
                    }
                    if (_loc_12)
                    {
                        _loc_13 = 0;
                    }
                    else if (_loc_2.volume < 0.05)
                    {
                        var _loc_24:Number = 0.2;
                        _loc_2.volume = 0.2;
                        _loc_13 = _loc_24;
                    }
                    else
                    {
                        _loc_13 = _loc_2.volume;
                    }
                    _loc_2.mute = _loc_12;
                    _loc_3.setVolume(_loc_13);
                    _loc_5.mute = _loc_12;
                    _loc_5.volume = _loc_2.volume;
                    break;
                }
                case PlayerCommandSet.END_SET_VOLUME:
                {
                    _loc_8 = MathUtil.unit(_loc_10 as Number);
                    _loc_5.volume = _loc_8;
                    _loc_5.mute = _loc_8 <= 0;
                }
                case PlayerCommandSet.SET_VOLUME:
                {
                    _loc_8 = MathUtil.unit(_loc_10 as Number);
                    _loc_2.volume = _loc_8;
                    _loc_2.mute = _loc_8 <= 0;
                    _loc_3.setVolume(_loc_8);
                    break;
                }
                case PlayerCommandSet.END_SET_BRIGHTNESS:
                {
                    _loc_5.brightness = Number(_loc_10);
                }
                case PlayerCommandSet.SET_BRIGHTNESS:
                {
                    _loc_8 = Number(_loc_10);
                    _loc_2.brightness = _loc_8;
                    _loc_3.setBrightness(_loc_8);
                    break;
                }
                case PlayerCommandSet.SET_VIDEO_SCALE:
                {
                    _loc_14 = Number(_loc_10);
                    if (isNaN(_loc_14) || _loc_14 <= 0)
                    {
                        break;
                    }
                    _loc_2.videoScale = _loc_14;
                    _loc_3.setVideoScale(_loc_14);
                    break;
                }
                case PlayerCommandSet.SET_ORIGINAL_VIDEO_SCALE:
                {
                    _loc_2.videoScale = _loc_2.orginalVideoScale;
                    _loc_3.setOriginalVideoScale(true);
                    break;
                }
                case PlayerCommandSet.SET_VIDEO_SIZE:
                {
                    _loc_15 = int(_loc_10);
                    _loc_2.fullFillVideo = _loc_15 == VideoSizeType.FULL;
                    _loc_3.setVideoSize(_loc_15);
                    break;
                }
                case PlayerCommandSet.SET_VIDEO_ROTATE:
                {
                    _loc_2.rotateType = int(_loc_10);
                    break;
                }
                case PlayerCommandSet.SET_CLOCK_WISE:
                {
                    _loc_16 = _loc_2.rotateType;
                    _loc_17 = Boolean(_loc_10);
                    if (_loc_17)
                    {
                        _loc_16++;
                    }
                    else
                    {
                        _loc_16 = _loc_16 - 1;
                    }
                    _loc_16 = (_loc_16 + 4) % 4;
                    _loc_2.rotateType = _loc_16;
                    _loc_5.rotateType = _loc_16;
                    break;
                }
                case PlayerCommandSet.PREVIEW:
                {
                    break;
                }
                case PlayerCommandSet.PREVIEW_READY:
                {
                    break;
                }
                case PlayerCommandSet.CLOSE_PREVIEW:
                {
                    break;
                }
                case PlayerCommandSet.BUTTON_LOG:
                {
                    HomeTrack.ins.addButtonLog(NodeLog.BUTTON_CLICK, _loc_10 as String);
                    break;
                }
                case PlayerCommandSet.VIDEO_FINISH:
                {
                    if (_loc_6.isListPlayer)
                    {
                        sendNotification(PlayListCommandSet.NEXT_ITEM);
                    }
                    else
                    {
                        if (_loc_6.firstFinish)
                        {
                            _loc_6.firstFinish = false;
                            ADTrackSender.getInstance().adExpect(AdType.BACK);
                        }
                        sendNotification(AdCommandSet.AD_CPT_FLOAT_HIDE);
                    }
                    break;
                }
                case PlayerCommandSet.SHOW_PLAY_COMPLETE:
                {
                    _loc_3.setBaseVideoInfo(_loc_6);
                    _loc_3.showPlayComplete();
                    break;
                }
                case PlayerCommandSet.HIDE_PLAY_COMPLETE:
                {
                    _loc_3.hidePlayComplete();
                    break;
                }
                case PlayerCommandSet.VIDEO_LOAD_COMPLETE:
                {
                    _loc_4.addNodeLog(NodeLog.VIDEO_LOAD_COMPLETE);
                    break;
                }
                case PlayerCommandSet.REPLAY:
                {
                    sendNotification(AdBackCommandSet.CLEAR);
                    sendNotification(AdPauseCommandSet.CLEAR);
                    sendNotification(PlayerCommandSet.HIDE_PLAY_COMPLETE);
                    _loc_6.updateCvid();
                    _loc_2.setSkipHeadTailValue(skipHeadValue, skipTailValue, 0);
                    _loc_2.replay();
                    break;
                }
                case PlayerCommandSet.CHANGE_RATE_TYPE:
                {
                    _loc_18 = int(_loc_10);
                    if (_loc_9 == SkinMediator.NAME)
                    {
                        StatusInfo.getInst().hasSelectRate = true;
                        if (_loc_6.isListPlayer)
                        {
                            _loc_5.listRateType = _loc_18;
                        }
                    }
                    _loc_6.currRateType = _loc_18;
                    _loc_19 = facade.retrieveProxy(PageProxy.NAME) as PageProxy;
                    _loc_2.rateType = _loc_18;
                    _loc_3.setRateType(_loc_18);
                    _loc_4.addNodeLog(NodeLog.CHANGE_RATE, [_loc_18]);
                    break;
                }
                case PlayerCommandSet.RATE_SETTING:
                {
                    processRateSetting();
                    break;
                }
                case PlayerCommandSet.SET_COOKIE_RATE:
                {
                    _loc_20 = _loc_10 as RateSettingData;
                    _loc_5.rateSetting = _loc_20;
                    sendNotification(PlayerCommandSet.RATE_SETTING);
                    _loc_4.addNodeLog(NodeLog.SAVE_COOKIE_RATE, [_loc_20.type, _loc_20.rateType]);
                    break;
                }
                case PlayerCommandSet.INIT_INFO:
                {
                    _loc_3.setMaxRate(_loc_6.maxRateType, _loc_6.original);
                    _loc_4.addNodeLog(NodeLog.HAS_HD, [_loc_6.maxRateType, _loc_6.original]);
                    _loc_3.setOriginalWidthHeight(_loc_6.originalWidth, _loc_6.originalHeight);
                    _loc_3.setOriginalRate(_loc_6.originalRate);
                    _loc_3.setSkipHeadTailValue(_loc_6.skipHeadValue, _loc_6.skipTailValue);
                    _loc_3.hasSkipHeadTail(_loc_6.skipHeadTail);
                    _loc_3.setSkipSeting(_loc_5.skipSetting);
                    _loc_3.setPlaylistSingleContinue(_loc_5.playListSingleContinue);
                    _loc_2.setSkipHeadTailValue(skipHeadValue, skipTailValue, lastCurPlayTimePos);
                    break;
                }
                case PlayerCommandSet.SET_COOKIE_SKIP:
                {
                    _loc_5.setUsed(CookieConsts.NO_FIRST_SKIP_TIP, true);
                    _loc_21 = _loc_10 as SkipSettingData;
                    _loc_5.skipSetting = _loc_21;
                    _loc_3.setSkipSeting(_loc_21);
                    _loc_3.showSettingedSkipTip();
                    sendNotification(PlayerCommandSet.SKIP_SETTING);
                    _loc_22 = [];
                    _loc_22.push(_loc_21.skipIntro ? (1) : (0));
                    _loc_22.push(_loc_21.skipOutro ? (1) : (0));
                    _loc_4.addNodeLog(NodeLog.SAVE_COOKIE_SKIP, _loc_22);
                    break;
                }
                case PlayerCommandSet.SKIP_SETTING:
                {
                    _loc_2.setSkipHeadTailValue(skipHeadValue, skipTailValue, 0);
                    break;
                }
                case PlayerCommandSet.SKIPPING_HEAD:
                {
                    _loc_4.addNodeLog(NodeLog.SKIPPING, [SkipType.head]);
                    break;
                }
                case PlayerCommandSet.SKIPPING_TAIL:
                {
                    _loc_4.addNodeLog(NodeLog.SKIPPING, [SkipType.tail]);
                    break;
                }
                case ViewCommandSet.ENTER_FULLSCREEN_AND_RESIZE:
                {
                    _loc_3.goTransformSkin();
                    processSizeRate(ViewState.FULLSCREEN);
                    break;
                }
                case ViewCommandSet.NORMALSCREEN_RESIZE:
                {
                    _loc_3.goTransformSkin();
                    break;
                }
                case ViewCommandSet.EXPAND_STAGE:
                {
                    _loc_3.setExand(_loc_10);
                    processSizeRate(_loc_10 ? (ViewState.EXPAND) : (ViewState.NORMAL));
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

        private function initPlayer() : void
        {
            var _loc_4:Number = NaN;
            var _loc_1:* = facade.retrieveProxy(PlayerProxy.NAME) as PlayerProxy;
            var _loc_2:* = facade.retrieveMediator(SkinMediator.NAME) as SkinMediator;
            var _loc_3:* = PlayerSO.getInst();
            _loc_4 = _loc_3.volume;
            if (isNaN(_loc_4))
            {
                _loc_4 = 1;
            }
            var _loc_5:* = _loc_3.mute;
            _loc_1.volume = _loc_4;
            _loc_1.mute = _loc_5;
            _loc_2.setVolume(_loc_5 ? (0) : (_loc_4));
            _loc_2.setMute(_loc_5);
            _loc_4 = _loc_3.brightness;
            if (isNaN(_loc_4))
            {
                _loc_4 = 0;
            }
            sendNotification(PlayerCommandSet.SET_BRIGHTNESS, _loc_4);
            return;
        }// end function

    }
}
